/**
 * Copyright (c) 2006-2007 Jinyi.lu
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of GNU LGPL license by gun.org
 * which accompanies this distribution, and is available at
 * http://gnu.feld-it.at/copyleft/lesser.html
 * 
 * Project Homepage : http://code.google.com/p/demou
 * Source Code: svn checkout http://demou.googlecode.com/svn/trunk/ demou
 * Mailing list : jinyi.lu@gmail.com
 *                stonelu@126.com
 *
 *  
 * Contributors:
 *     JinYi.Lu - initial API and implementation
 */
package cn.tworen.demou.remoting;

import org.apache.log4j.Logger;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.tworen.demou.Config;


public class Txm<T>{
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(Txm.class);

	public static ApplicationContext context = WebServiceContext.appContext;

	private static final Txm manager = new Txm();

	public static Txm getTxm() {
		return manager;
	}

	private Txm() {
		initContext();
	}

	public ApplicationContext getContext() {
		if (context == null)
			initContext();
		return context;
	}

	public T getBean(String key) {
		Object obj = getContext().getBean(key);
		try {
			if (obj instanceof FactoryBean) {
				obj = ((ProxyFactoryBean) obj).getTargetSource().getTarget();
			}
		} catch (Exception ex) {
			logger.error(ex);
		}
		return (T)obj;
	}

	private void initContext() {
		if (context == null) {
			try {
				context = new ClassPathXmlApplicationContext(Config.getInstance().read("Context.File"));
			} catch (Exception ex) {
				logger.error(ex);
			}
		}
	}
//	public Service getService(String name) {
//		return (Service)this.manager.getBean(name + "Service");
//	}
	public static void main(String[] argv){
		
	}
}